home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / gnuplot / plot.c < prev    next >
C/C++ Source or Header  |  1993-09-15  |  13KB  |  494 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: plot.c%v 3.50.1.8 1993/07/27 05:37:15 woo Exp $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - plot.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software is provided "as is" without express or implied warranty.
  21.  * 
  22.  *
  23.  * AUTHORS
  24.  * 
  25.  *   Original Software:
  26.  *     Thomas Williams,  Colin Kelley.
  27.  * 
  28.  *   Gnuplot 2.0 additions:
  29.  *       Russell Lang, Dave Kotz, John Campbell.
  30.  *
  31.  *   Gnuplot 3.0 additions:
  32.  *       Gershon Elber and many others.
  33.  * 
  34.  * There is a mailing list for gnuplot users. Note, however, that the
  35.  * newsgroup 
  36.  *    comp.graphics.gnuplot 
  37.  * is identical to the mailing list (they
  38.  * both carry the same set of messages). We prefer that you read the
  39.  * messages through that newsgroup, to subscribing to the mailing list.
  40.  * (If you can read that newsgroup, and are already on the mailing list,
  41.  * please send a message info-gnuplot-request@dartmouth.edu, asking to be
  42.  * removed from the mailing list.)
  43.  *
  44.  * The address for mailing to list members is
  45.  *       info-gnuplot@dartmouth.edu
  46.  * and for mailing administrative requests is 
  47.  *       info-gnuplot-request@dartmouth.edu
  48.  * The mailing list for bug reports is 
  49.  *       bug-gnuplot@dartmouth.edu
  50.  * The list of those interested in beta-test versions is
  51.  *       info-gnuplot-beta@dartmouth.edu
  52.  */
  53.  
  54. #include <stdio.h>
  55. #include <setjmp.h>
  56. #include <signal.h>
  57. #ifdef XPG3_LOCALE
  58. #include <locale.h>
  59. #endif
  60. #include "plot.h"
  61. #include "setshow.h"
  62. #if defined(MSDOS) || defined(DOS386)
  63. #include <io.h>
  64. #endif
  65. #ifdef vms
  66. #include <unixio.h>
  67. #include <smgdef.h>
  68. extern int vms_vkid;
  69. extern smg$create_virtual_keyboard();
  70. unsigned int status[2] = {1, 0};
  71. #endif
  72. #ifdef AMIGA_SC_6_1
  73. #include <proto/dos.h>
  74. #endif
  75.  
  76. #ifdef _Windows
  77. #include <windows.h>
  78. #ifndef SIGINT
  79. #define SIGINT 2    /* for MSC */
  80. #endif
  81. #else
  82. # ifdef __TURBOC__
  83. # include <graphics.h>
  84. # endif
  85. #endif
  86.  
  87. #ifndef AMIGA_SC_6_1
  88. extern char *getenv(),*strcat(),*strcpy(),*strncpy();
  89. #endif /* !AMIGA_SC_6_1 */
  90.  
  91. extern char input_line[];
  92. extern int c_token;
  93. extern FILE *outfile;
  94. extern int term;
  95.  
  96. TBOOLEAN interactive = TRUE;    /* FALSE if stdin not a terminal */
  97. TBOOLEAN noinputfiles = TRUE;    /* FALSE if there are script files */
  98. char *infile_name = NULL;    /* name of command file; NULL if terminal */
  99.  
  100. #ifndef STDOUT
  101. #define STDOUT 1
  102. #endif
  103.  
  104. #ifdef _Windows
  105. jmp_buf far env;
  106. #else
  107. jmp_buf env;
  108. #endif
  109.  
  110. struct value *Ginteger(),*Gcomplex();
  111.  
  112.  
  113. extern f_push(),f_pushc(),f_pushd1(),f_pushd2(),f_pushd(),f_call(),f_calln(),
  114.     f_lnot(),f_bnot(),f_uminus(),f_lor(),f_land(),f_bor(),f_xor(),
  115.     f_band(),f_eq(),f_ne(),f_gt(),f_lt(),
  116.     f_ge(),f_le(),f_plus(),f_minus(),f_mult(),f_div(),f_mod(),f_power(),
  117.     f_factorial(),f_bool(),f_jump(),f_jumpz(),f_jumpnz(),f_jtern();
  118.  
  119. extern f_real(),f_imag(),f_arg(),f_conjg(),f_sin(),f_cos(),f_tan(),f_asin(),
  120.     f_acos(),f_atan(),f_sinh(),f_cosh(),f_tanh(),f_int(),f_abs(),f_sgn(),
  121.     f_sqrt(),f_exp(),f_log10(),f_log(),f_besj0(),f_besj1(),f_besy0(),f_besy1(),
  122.     f_erf(), f_erfc(), f_gamma(), f_lgamma(), f_ibeta(), f_igamma(), f_rand(),
  123.     f_floor(),f_ceil(),
  124.     f_normal(), f_inverse_erf(), f_inverse_normal();   /* XXX - JG */
  125.  
  126.  
  127. struct ft_entry GPFAR ft[] = {    /* built-in function table */
  128.  
  129. /* internal functions: */
  130.     {"push", f_push},    {"pushc", f_pushc},
  131.     {"pushd1", f_pushd1},    {"pushd2", f_pushd2},    {"pushd", f_pushd},
  132.     {"call", f_call},    {"calln", f_calln},    {"lnot", f_lnot},
  133.     {"bnot", f_bnot},    {"uminus", f_uminus},    {"lor", f_lor},
  134.     {"land", f_land},    {"bor", f_bor},        {"xor", f_xor},
  135.     {"band", f_band},    {"eq", f_eq},        {"ne", f_ne},
  136.     {"gt", f_gt},        {"lt", f_lt},        {"ge", f_ge},
  137.     {"le", f_le},        {"plus", f_plus},    {"minus", f_minus},
  138.     {"mult", f_mult},    {"div", f_div},        {"mod", f_mod},
  139.     {"power", f_power}, {"factorial", f_factorial},
  140.     {"bool", f_bool},    {"jump", f_jump},    {"jumpz", f_jumpz},
  141.     {"jumpnz",f_jumpnz},{"jtern", f_jtern},
  142.  
  143. /* standard functions: */
  144.     {"real", f_real},    {"imag", f_imag},    {"arg", f_arg},
  145.     {"conjg", f_conjg}, {"sin", f_sin},        {"cos", f_cos},
  146.     {"tan", f_tan},        {"asin", f_asin},    {"acos", f_acos},
  147.     {"atan", f_atan},    {"sinh", f_sinh},    {"cosh", f_cosh},
  148.     {"tanh", f_tanh},    {"int", f_int},        {"abs", f_abs},
  149.     {"sgn", f_sgn},        {"sqrt", f_sqrt},    {"exp", f_exp},
  150.     {"log10", f_log10},    {"log", f_log},        {"besj0", f_besj0},
  151.     {"besj1", f_besj1},    {"besy0", f_besy0},    {"besy1", f_besy1},
  152.         {"erf", f_erf},         {"erfc", f_erfc},       {"gamma", f_gamma},     {"lgamma", f_lgamma},
  153.         {"ibeta", f_ibeta},     {"igamma", f_igamma},   {"rand", f_rand},
  154.         {"floor", f_floor},     {"ceil", f_ceil},
  155.  
  156.     {"norm",        f_normal},              /* XXX-JG */
  157.     {"inverf",      f_inverse_erf},         /* XXX-JG */
  158.     {"invnorm",     f_inverse_normal},      /* XXX-JG */
  159.  
  160.     {NULL, NULL}
  161. };
  162.  
  163. static struct udvt_entry udv_pi = {NULL, "pi",FALSE};
  164.                                     /* first in linked list */
  165. struct udvt_entry *first_udv = &udv_pi;
  166. struct udft_entry *first_udf = NULL;
  167.  
  168.  
  169.  
  170. #ifdef vms
  171.  
  172. #define HOME "sys$login:"
  173.  
  174. #else /* vms */
  175. #if defined(MSDOS) ||  defined(AMIGA_AC_5) || defined(AMIGA_SC_6_1) || defined(ATARI) || defined(OS2) || defined(_Windows) || defined(DOS386)
  176.  
  177. #define HOME "GNUPLOT"
  178.  
  179. #else /* MSDOS || AMIGA || ATARI || OS2 || _Windows || defined(DOS386)*/
  180.  
  181. #define HOME "HOME"
  182.  
  183. #endif /* MSDOS || AMIGA || ATARI || OS2 || _Windows || defined(DOS386)*/
  184. #endif /* vms */
  185.  
  186. #if defined(unix) || defined(AMIGA_AC_5) || defined(AMIGA_SC_6_1)
  187. #define PLOTRC ".gnuplot"
  188. #else /* AMIGA || unix */
  189. #define PLOTRC "gnuplot.ini"
  190. #endif /* AMIGA || unix */
  191.  
  192. #if defined (__TURBOC__) || defined (__PUREC__)
  193. void tc_interrupt()
  194. #else
  195. #ifdef __ZTC__
  196. void ztc_interrupt()
  197. #else
  198. #if defined( _CRAY ) || defined( sgi ) || defined( __alpha )
  199. void inter(an_int)
  200. int an_int;
  201. #else
  202. #if defined( NEXT ) || defined( OS2 ) || defined( VMS )
  203. void inter(int an_int)
  204. #else
  205. #ifdef sgi
  206. void inter(int sig, int code, struct sigcontext *sc)
  207. #else
  208. #if defined(SOLARIS)
  209. void inter()
  210. #else
  211. inter()
  212. #endif
  213. #endif
  214. #endif
  215. #endif
  216. #endif
  217. #endif
  218. {
  219. #if defined (MSDOS) || defined(_Windows) || (defined (ATARI) && defined(__PUREC__)) || defined(DOS386)
  220. #if defined (__TURBOC__) || defined (__PUREC__)
  221. #ifndef DOSX286
  222.     (void) signal(SIGINT, tc_interrupt);
  223. #endif
  224. #else
  225. #ifdef __ZTC__
  226.    (void) signal(SIGINT, ztc_interrupt);
  227. #else
  228. #ifdef __EMX__
  229.     (void) signal(SIGINT, (void *)inter);
  230. #else
  231. #ifdef DJGPP
  232.     (void) signal(SIGINT, (SignalHandler)inter);
  233. #else
  234. #if defined __MSC__
  235.     (void) signal(SIGINT, inter);
  236. #endif    /* __MSC__ */
  237.  
  238. #endif    /* DJGPP */
  239. #endif  /* __EMX__ */
  240. #endif    /* ZTC */
  241. #endif  /* __TURBOC__ */
  242.  
  243. #else  /* MSDOS */
  244. #ifdef OS2
  245.     (void) signal(an_int, SIG_ACK);
  246. #else
  247.     (void) signal(SIGINT, inter);
  248. #endif  /* OS2 */
  249. #endif  /* MSDOS */
  250. #ifndef DOSX286
  251.     (void) signal(SIGFPE, SIG_DFL);    /* turn off FPE trapping */
  252. #endif
  253.     if (term && term_init)
  254.         (*term_tbl[term].text)();    /* hopefully reset text mode */
  255.     (void) fflush(outfile);
  256.     (void) putc('\n',stderr);
  257.     longjmp(env, TRUE);        /* return to prompt */
  258. }
  259.  
  260.  
  261. #ifdef _Windows
  262. gnu_main(argc, argv)
  263. #else
  264. main(argc, argv)
  265. #endif
  266.     int argc;
  267.     char **argv;
  268. {
  269. #ifdef XPG3_LOCALE
  270.     (void) setlocale(LC_CTYPE, "");
  271. #endif
  272. /* Register the Borland Graphics Interface drivers. If they have been */
  273. /* included by the linker.                                            */
  274.  
  275. #ifndef DOSX286
  276. #ifndef _Windows
  277. #if defined (__TURBOC__) && defined (MSDOS)
  278. registerfarbgidriver(EGAVGA_driver_far);
  279. registerfarbgidriver(CGA_driver_far);
  280. registerfarbgidriver(Herc_driver_far);
  281. registerfarbgidriver(ATT_driver_far);
  282. # endif
  283. #endif
  284. #endif
  285. #ifdef X11
  286.      { int n = X11_args(argc, argv); argv += n; argc -= n; }
  287. #endif 
  288.  
  289. #ifdef apollo
  290.     apollo_pfm_catch();
  291. #endif
  292.  
  293.     setbuf(stderr,(char *)NULL);
  294. #ifdef UNIX
  295.     setlinebuf(stdout);
  296. #endif
  297.     outfile = stdout;
  298.     (void) Gcomplex(&udv_pi.udv_value, Pi, 0.0);
  299.  
  300.      interactive = FALSE;
  301.      init_terminal();        /* can set term type if it likes */
  302.  
  303. #ifdef AMIGA_SC_6_1
  304.      if (IsInteractive(Input()) == DOSTRUE) interactive = TRUE;
  305.      else interactive = FALSE;
  306. #else
  307. #if defined(__MSC__) && defined(_Windows)
  308.      interactive = TRUE;
  309. #else
  310.      interactive = isatty(fileno(stdin));
  311. #endif
  312. #endif
  313.      if (argc > 1)
  314.       interactive = noinputfiles = FALSE;
  315.      else
  316.       noinputfiles = TRUE;
  317.  
  318.      if (interactive)
  319.       show_version();
  320. #ifdef vms   /* initialise screen management routines for command recall */
  321.           if (status[1] = smg$create_virtual_keyboard(&vms_vkid) != SS$_NORMAL)
  322.                done(status[1]);
  323. #endif
  324.  
  325.     if (!setjmp(env)) {
  326.         /* first time */
  327.         interrupt_setup();
  328.         load_rcfile();
  329.  
  330.         if (interactive && term != 0)    /* not unknown */
  331.          fprintf(stderr, "\nTerminal type set to '%s'\n", 
  332.                 term_tbl[term].name);
  333.     } else {    
  334.         /* come back here from int_error() */
  335.         load_file_error();    /* if we were in load_file(), cleanup */
  336. #ifdef _Windows
  337.     SetCursor(LoadCursor((HINSTANCE)NULL, IDC_ARROW));
  338. #endif
  339. #ifdef vms
  340.         /* after catching interrupt */
  341.         /* VAX stuffs up stdout on SIGINT while writing to stdout,
  342.           so reopen stdout. */
  343.         if (outfile == stdout) {
  344.            if ( (stdout = freopen("SYS$OUTPUT","w",stdout))  == NULL) {
  345.               /* couldn't reopen it so try opening it instead */
  346.               if ( (stdout = fopen("SYS$OUTPUT","w"))  == NULL) {
  347.                  /* don't use int_error here - causes infinite loop! */
  348.                  fprintf(stderr,"Error opening SYS$OUTPUT as stdout\n");
  349.               }
  350.            }
  351.            outfile = stdout;
  352.         }
  353. #endif                    /* VMS */
  354.         if (!interactive && !noinputfiles) {
  355.             if (term && term_init)
  356.                 (*term_tbl[term].reset)();
  357. #ifdef vms
  358.             vms_reset();
  359. #endif
  360.             return(IO_ERROR);    /* exit on non-interactive error */
  361.          }
  362.     }
  363.  
  364.      if (argc > 1) {
  365.         /* load filenames given as arguments */
  366.         while (--argc > 0) {
  367.            ++argv;
  368.            c_token = NO_CARET; /* in case of file not found */
  369.            load_file(fopen(*argv,"r"), *argv);    
  370.         }
  371.     } else {
  372.         /* take commands from stdin */
  373.         while(!com_line());
  374.     }
  375.  
  376.     if (term && term_init)
  377.         (*term_tbl[term].reset)();
  378. #ifdef vms
  379.     vms_reset();
  380. #endif
  381.     return(IO_SUCCESS);
  382. }
  383.  
  384. #if defined(ATARI) && defined(__PUREC__)
  385. #include <math.h>
  386. int purec_matherr(struct exception *e)
  387. {    char *c;
  388.     switch (e->type) {
  389.         case DOMAIN:    c = "domain error"; break;
  390.         case SING  :    c = "argument singularity"; break;
  391.         case OVERFLOW:  c = "overflow range"; break;
  392.         case UNDERFLOW: c = "underflow range"; break;
  393.         default:        c = "(unknown error"; break;
  394.     }
  395.     fprintf(stderr, "math exception : %s\n", c);
  396.     fprintf(stderr, "    name : %s\n", e->name);
  397.     fprintf(stderr, "    arg 1: %e\n", e->arg1);
  398.     fprintf(stderr, "    arg 2: %e\n", e->arg2);
  399.     fprintf(stderr, "    ret  : %e\n", e->retval);
  400.     return 1;
  401. }
  402. #endif
  403.  
  404. /* Set up to catch interrupts */
  405. interrupt_setup()
  406. {
  407. #if defined (MSDOS) || defined(_Windows) || (defined (ATARI) && defined(__PUREC__)) || defined(DOS386)
  408. #ifdef __PUREC__
  409.     setmatherr(purec_matherr);
  410. #endif
  411. #if defined (__TURBOC__) || defined (__PUREC__)
  412. #if !defined(DOSX286) && !defined(BROKEN_SIGINT)
  413.         (void) signal(SIGINT, tc_interrupt);    /* go there on interrupt char */
  414. #endif
  415. #else
  416. #ifdef __ZTC__
  417.         (void) signal(SIGINT, ztc_interrupt);
  418. #else
  419. #ifdef __EMX__
  420.         (void) signal(SIGINT, (void *)inter);    /* go there on interrupt char */
  421. #else
  422. #ifdef DJGPP
  423.         (void) signal(SIGINT, (SignalHandler)inter);    /* go there on interrupt char */
  424. #else
  425.                (void) signal(SIGINT, inter);
  426. #endif
  427. #endif
  428. #endif
  429. #endif
  430. #else /* MSDOS */
  431.         (void) signal(SIGINT, inter);    /* go there on interrupt char */
  432. #endif /* MSDOS */
  433. }
  434.  
  435.  
  436. /* Look for a gnuplot start-up file */
  437. load_rcfile()
  438. {
  439.     register FILE *plotrc;
  440.     char home[80]; 
  441.     char rcfile[sizeof(PLOTRC)+80];
  442.  
  443.     /* Look for a gnuplot init file in . or home directory */
  444. #ifdef vms
  445.     (void) strcpy(home,HOME);
  446. #else /* vms */
  447.     char *tmp_home=getenv(HOME);
  448.     char *p;    /* points to last char in home path, or to \0, if none */
  449.     char c='\0';/* character that should be added, or \0, if none */
  450.  
  451.  
  452.     if(tmp_home) {
  453.         strcpy(home,tmp_home);
  454.     if( strlen(home) ) p = &home[strlen(home)-1];
  455.     else           p = home;
  456. #if defined(MSDOS) || defined(ATARI) || defined( OS2 ) || defined(_Windows) || defined(DOS386)
  457.     if( *p!='\\' && *p!='\0' ) c='\\';
  458. #else
  459. #if defined(AMIGA_AC_5)
  460.     if( *p!='/' && *p!=':' && *p!='\0' ) c='/';
  461. #else /* that leaves unix */
  462.     c='/';
  463. #endif
  464. #endif
  465.     if(c) {
  466.         if(*p) p++;
  467.         *p++=c;
  468.         *p='\0';
  469.     }
  470.     }
  471. #endif /* vms */
  472.  
  473. #ifdef NOCWDRC
  474.     /* inhibit check of init file in current directory for security reasons */
  475.     {
  476. #else
  477.     (void) strcpy(rcfile, PLOTRC);
  478.     plotrc = fopen(rcfile,"r");
  479.     if (plotrc == (FILE *)NULL) {
  480. #endif
  481. #ifndef vms
  482.     if( tmp_home ) {
  483. #endif
  484.        (void) sprintf(rcfile, "%s%s", home, PLOTRC);
  485.        plotrc = fopen(rcfile,"r");
  486. #ifndef vms
  487.     } else
  488.         plotrc=NULL;
  489. #endif
  490.     }
  491.     if (plotrc)
  492.      load_file(plotrc, rcfile);
  493. }
  494.